import { Controller, Get, Query, Headers, Post, Body, Patch, Param, Delete, HttpCode, Header, HttpException, Catch, HttpStatus } from "@nestjs/common";
import { ForbiddenException } from "src/common/filters/forbidden.exception";
import { HelloService } from "./hello.service";

@Controller('/hello')
export class HelloController {
  constructor(private readonly helloService: HelloService){}

  // query 查询
  @Get('')
  @HttpCode(203)
  fetch(@Query(){id}, @Headers('token') token): string {
    // console.log(token);
    if (!id) {
      // 自定义内置HTTP异常1
      throw new ForbiddenException()
      // 全局自定义异常
      // throw new HttpException(
      //   {message: 'id 不能为空', error: 'id is required'},
      //   HttpStatus.FORBIDDEN
      // )

    }
    return this.helloService.fetch(id);
  }

  // body 新建
  @Post()
  @Header('Cache-Control', 'none')
  save(@Body() {id, name, message}) {
    return this.helloService.save(id, name, message)
  }

  // 更新
  @Patch(':id')
  update(@Param() { id }, @Body() { message }): string {
    return this.helloService.update(id, message);
  }

  // 删除
  @Delete()
  remove(@Query() { id }): string {
    return this.helloService.remove(id);
  }
}
